/**
 * @desc   格式化时间戳为年月日时分秒
 * @param  {Number,Date} date
 * @param  {String} fmt
 * @return {String}
 */
 function isDate(o) {
    return Object.prototype.toString.call(o) === "[object Date]";
  }
  
  /**
   * 时间戳转：xx天前，xx小时前，xx分钟前转换
   * @param {string} time 
   */
  const formatPreTime = (time) => {
    time = parseInt(time)
    var dateTime = new Date(time);
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var hour = dateTime.getHours();
    var minute = dateTime.getMinutes();
    var second = dateTime.getSeconds();
    var now = new Date().getTime();
    var milliseconds = 0;
    var timeStr;
    milliseconds = now - time;
    if (milliseconds <= 1000 * 60 * 1) {
      timeStr = '刚刚';
    } else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
      timeStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
    } else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
      timeStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
    } else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 3) {
      timeStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
    } else if (milliseconds > 1000 * 60 * 60 * 24 * 15) {
      timeStr = year + '-' + month + '-' + day;
    } else {
      timeStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
    }
    return timeStr;
  };
  
  /**
   * 时间戳转：固定日期格式日期
   * @param {string} date 时间
   * @param {string} type 类型
   * @param {string} symbol 拼接符 
   */
  const formatDateTime = (date, type, symbol) => {
    symbol = symbol ? symbol : '-';
    date = parseInt(date);
    var date = new Date(date);
    var YY = date.getFullYear();
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
    var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
    var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    if (type == 'YY') {
      return YY;
    }
    if (type == 'YYMM') {
      return YY + symbol + MM;
    }
    if (type == 'YYMMDD') {
      return YY + symbol + MM + symbol + DD;
    }
    if (type == 'YYMMDDhhmm') {
      return YY + symbol + MM + symbol + DD + " " + hh + symbol + mm;
    }
    if (type == 'YYMMDDhhmmss') {
      return YY + symbol + MM + symbol + DD + " " + hh + symbol + mm + symbol + ss;
    }
    if (type == 'hhmm') {
      return hh + symbol + mm;
    }
    if (type == 'hhmmss') {
      return hh + symbol + mm + symbol + ss;
    }
  }
  
  const timeAgo = (o, format = "yyyy-MM-dd") => {
    let n = new Date().getTime();
    //但在苹果上是不支持的，必须这样写：
    let f = null;
    if (o.indexOf("T") > -1) {
      f = (n - new Date(o).getTime()) / 1000;
    } else {
      f = (n - new Date(o.replace(/-/g, "/")).getTime()) / 1000;
    }
    if (parseInt(f) < 64) {
      return "刚刚";
    } // 小于60秒,刚刚
  
    let today = formatDate(new Date(), format);
    let day = formatDate(o, format);
    if (today == day) {
      if (o.indexOf("T") > -1) {
        return "今天" + (new Date(o) + "").slice(16, 21);
      } else {
        return "今天" + o.substr(11, 5);
      }
    } else {
      return formatDate(o, format);
    }
  };
  
  const timeAgeYear = (o) => {
    let format = 'MM-dd hh:mm'
    let n = new Date().getTime();
    //但在苹果上是不支持的，必须这样写：
    let f = null;
    if (o.indexOf("T") > -1) {
      f = (n - new Date(o).getTime()) / 1000;
    } else {
      f = (n - new Date(o.replace(/-/g, "/")).getTime()) / 1000;
    }
    if (parseInt(f) < 64) {
      return "刚刚";
    } // 小于60秒,刚刚
    if(new Date(o).getFullYear()!=new Date().getFullYear&&new Date(o).getMonth()<new Date().getMonth()){
      format='yyyy-MM-dd'
    }
  
    let today = formatDate(new Date(), format);
    let day = formatDate(o, format);
    if (today == day) {
      if (o.indexOf("T") > -1) {
        return "今天" + (new Date(o) + "").slice(16, 21);
      } else {
        return "今天" + o.substr(11, 5);
      }
    } else {
      return formatDate(o, format);
    }
  };
  
  //2021-07-23T13:39:32.000+0000时间格式转换
  const formatDate = (date, format = "yyyy-MM-dd hh:mm:ss") => {
    if (!date) {
      return "";
    } else {
      let time = new Date(date).getTime();
      return formatTime(time, format);
    }
  };
  
  //时间戳转换
  const formatTime = function (date) {
    var fmt =
      arguments.length > 1 && arguments[1] !== undefined
        ? arguments[1]
        : "yyyy-MM-dd hh:mm:ss";
    if (isDate(date) === false) {
      date = new Date(+date);
    }
    var o = {
      "M+": date.getMonth() + 1,
      "d+": date.getDate(),
      "h+": date.getHours(),
      "m+": date.getMinutes(),
      "s+": date.getSeconds(),
      "q+": Math.floor((date.getMonth() + 3) / 3),
      S: date.getMilliseconds(),
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        (date.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    for (var k in o) {
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
        );
    }
    return fmt;
  };
  
  const getWeekDate = (date) => {
    if (!date) {
      return "";
    } else {
      let now = new Date(date);
      let day = now.getDay();
      let weeks = new Array(
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六"
      );
      // console.log('weeks', day)
      let week = weeks[day];
      return week;
    }
  };
  
  //秒数转分钟
  const _dateMinutes = function (s) {
    let h;
    h = Math.floor(s / 60);
    s = s % 60;
    h += "";
    s += "";
    h = h.length == 1 ? "0" + h : h;
    s = s.length == 1 ? "0" + s : s;
    return h + ":" + s;
  };
  
  
  export default {
     formatPreTime,
     formatDateTime,
     timeAgo,
     timeAgeYear,
     formatDate,
     formatTime,
     getWeekDate,
     _dateMinutes,
  }